package com.unhz.taigu.controller;

import com.alibaba.fastjson.JSON;
import com.unhz.generator.pojo.LoginInfo;
import com.unhz.generator.utils.JsonResult;
import com.unhz.generator.utils.Page;
import com.unhz.generator.utils.PageSize;
import com.unhz.taigu.invohandler.LogInvoHandler;
import com.unhz.taigu.service.LoginInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * @Author: 曹显贵
 * @Description: 用户登录日志
 * @Date: Created in 19:48 2018/1/25 0025
 * @Modified By:
 */
@Controller
@RequestMapping("loginInfoController")
public class LoginInfoController {

    private final Logger logger = LoggerFactory.getLogger(LoginInfo.class);

    @Autowired
    private LoginInfoService loginInfoService;
    //LoginInfoService loginInfoService = LogInvoHandler.getProxyInstance(LoginInfoService.class);
    /**
     * 分页查询日志信息
     * @param model
     * @param pageNo
     * @param keywords
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/findPageAllLoginInfo")
    @RequiresPermissions("log:findAll")
    public String findPageAllLoginInfo(Model model,
                                       @RequestParam(value = "pageNo",defaultValue = "1",required = false)String pageNo,
                                       @RequestParam(value = "keywords",defaultValue = "",required = false)String keywords,
                                       HttpServletRequest request) throws UnsupportedEncodingException {
        if("GET".equals(request.getMethod())){
            keywords = new String(keywords.getBytes("iso-8859-1"),"utf-8");
            logger.info("转换格式");
        }
        Page<LoginInfo> page = loginInfoService.findPageAllLoginInfo(pageNo, PageSize.pageSize,keywords);
        model.addAttribute("page",page);
        model.addAttribute("keywords",keywords);
        return "system/logininfo/login_info-list.jsp";
    }

    @RequestMapping("/delete")
    @RequiresPermissions("log:delete")
    @ResponseBody
    public JsonResult delete(@RequestParam("loginInfoId")Integer loginInfoId){
        int count = loginInfoService.deleteById(loginInfoId);
        if(count > 0){
            return JsonResult.success("删除成功");
        }
        return JsonResult.failure("删除失败");
    }

    @RequestMapping("/addLoginInfo")
    @ResponseBody
    public JsonResult addLoginInfo(LoginInfo loginInfo,HttpServletRequest request){
        //获取访问者ip(一般通用)
        String userIp = request.getRemoteAddr();
        loginInfo.setIpAddress(userIp);
        logger.info("你的访问ip:"+userIp);
        int count = loginInfoService.insertLoginInfo(loginInfo);
        //记录日志
        return JsonResult.success(userIp);
    }

}
